/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.1 $
 * * DATE
 * *    $Date: 2007/06/13 00:44:59 $
 * * LOG
 * *    $Log: StringHelper.java,v $
 * *    Revision 1.1  2007/06/13 00:44:59  animesh
 * *    more serialization done.
 * *
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/
package atag.common;

/**
 * This class is for helper methods for Strings, mainly those that 
 * are absent in j2me and used in ATaG
 * @author Animesh
 */
public class StringHelper {
    
    /** Creates a new instance of StringHelper */
    public StringHelper() {
    }
    
    
    /**
     * splits a string and returns an array
     * @param s The string to be split
     * @param c The character that is the delimiter
     * @returns An array of strings, obtained by splitting s using c
     */
    public static String[] splitString(String s, char c){
        int startIndex = 0;
        int endIndex = 0;
        int count=0; //The number of substrings found so far
        String[] tempArr = new String[s.length()];
        
        // do the stuff
        while(true){
            endIndex = s.indexOf(c,startIndex);
            
            if(endIndex != -1){
                tempArr[count] = s.substring(startIndex, endIndex);
                startIndex = endIndex+1;
                count++;
            }else{
                //no more found, this is the last such substring
                tempArr[count] = s.substring(startIndex);
                count++;
                break;
            }
        }
        String[] retArr = new String[count];
        for(int i=0; i<count; i++){
            retArr[i] = tempArr[i];
        }
        
        return retArr;
        
    }
 
    /**
     * for testing
     */
    public static void main(String[] args){
        
        String testString = "This:is:a:string";
        
        String[] splits = StringHelper.splitString(testString, ':');
        
        for(int i=0; i<splits.length; i++){
            System.out.println(splits[i]);
        }
        
    }
    
    
}
